package com.logic.modular.gameCore.cultivate.skill.service;


import com.baomidou.mybatisplus.extension.service.IService;
import com.comment.pojo.domain.logic.core.CoreSkill;
import com.logic.modular.gameCore.cultivate.skill.mapper.CoreSkillMapper;
import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;


/**
 * 战法表(CoreSkill)表服务实现类
 *
 * @author cxy
 * @since 2024-08-05 16:14:02
 */

@Slf4j
@Service
public class CoreSkillServiceImpl extends ServiceImpl<CoreSkillMapper, CoreSkill> implements IService<CoreSkill> {
    private static final Cache<String, Map<Long, CoreSkill>> cache = Caffeine.newBuilder().build();

    public List<CoreSkill> eqList(CoreSkill select) {
        return listCache().stream().filter(item ->
                select.getId() == null || Objects.equals(item.getId(), select.getId())
                        && select.getName() == null || Objects.equals(item.getName(), select.getName())
                        && select.getType() == null || Objects.equals(item.getType(), select.getType())
                        && select.getQualityLv() == null || Objects.equals(item.getQualityLv(), select.getQualityLv())
                        && select.getIntro() == null || Objects.equals(item.getIntro(), select.getIntro())
                        && select.getArms() == null || Objects.equals(item.getArms(), select.getArms())
                        && select.getIsMagic() == null || Objects.equals(item.getIsMagic(), select.getIsMagic())
                        && select.getMagicInfluence() == null || Objects.equals(item.getMagicInfluence(), select.getMagicInfluence())
                        && select.getIcon() == null || Objects.equals(item.getIcon(), select.getIcon())
        ).collect(Collectors.toList());
    }

    public CoreSkill eqSelect(CoreSkill select) {
        return eqList(select).stream().findFirst().orElse(null);
    }

    @PostConstruct
    private void init() {
        CoreSkill initSelect = CoreSkill.builder().build();
        List<CoreSkill> list = this.list(new LambdaQueryWrapper<>(initSelect));
        log.info("开始缓存战法表数据,数据量为:{}", list.size());
        cache.put(
                "coreSkill",
                list.stream().collect(Collectors.toMap(CoreSkill::getId, CoreSkill -> CoreSkill))
        );
        log.info("战法表数据缓存完成");
    }

    public static List<CoreSkill> listCache() {
        Map<Long, CoreSkill> values = cache.getIfPresent("coreSkill");
        if (Objects.isNull(values)) {
            throw new RuntimeException("战法表数据为空");
        }
        return new ArrayList<>(values.values());
    }

    public static List<CoreSkill> listCacheByLv(Integer lv) {
        Map<Integer, List<CoreSkill>> collect = listCache().stream().collect(Collectors.groupingBy(CoreSkill::getQualityLv));
        return collect.get(lv);
    }

    public Map<Long, CoreSkill> mapCache() {
        Map<Long, CoreSkill> values = cache.getIfPresent("coreSkill");
        if (Objects.isNull(values)) {
            throw new RuntimeException("战法表数据为空");
        }
        return values;
    }

    public static CoreSkill getCache(Long id) {
        Map<Long, CoreSkill> values = cache.getIfPresent("coreSkill");
        if (Objects.isNull(values)) {
            throw new RuntimeException("战法表数据为空");
        }
        return values.get(id);
    }

}
